home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 4: GNU Archives / Linux Cubed Series 4 - GNU Archives.iso / gnu / graphics.17 / graphics / graphics-0.17 / tek2plot / tek2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-03-12  |  11.1 KB  |  515 lines

  1. /* This program, plot, translates a GNU plot file into device specific output.
  2.    Copyright (C) 1989 Free Software Foundation, Inc.
  3.  
  4. Plot is distributed in the hope that it will be useful, but WITHOUT
  5. ANY WARRANTY.  No author or distributor accepts responsibility to
  6. anyone for the consequences of using it or for whether it serves any
  7. particular purpose or works at all, unless he says so in writing.
  8. Refer to the GNU General Public License for full details.
  9.  
  10. Everyone is granted permission to copy, modify and redistribute plot,
  11. but only under the conditions described in the GNU General Public
  12. License.  A copy of this license is supposed to have been given to you
  13. along with plot so you can know your rights and responsibilities.  It
  14. should be in a file named COPYING.  Among other things, the copyright
  15. notice and this notice must be preserved on all copies.  */
  16.  
  17. /* This file is the main routine for plot.
  18.  
  19.    It includes code to read the plot file and call plot functions
  20.    to draw the graphics. */
  21.  
  22. #include "sys-defines.h"
  23. #include "libplot.h"
  24. #include "getopt.h"
  25. #include "../COPYING"
  26.  
  27. int read_byte (stream)
  28.      FILE *stream;
  29. {
  30.   int i;
  31.   i = (unsigned) getc (stream);
  32.   /* printf ("<%o %o %o %c>", i>>5, i&0x1F, i, i);*/
  33.   return i;
  34. }
  35.  
  36. /* read_plot reads a plot file from the standard input and calls
  37.    a plot function according to each plot instruction found in the
  38.    file. */
  39.  
  40. void
  41. read_plot (in_stream, buffer, buffer_length)
  42.      FILE *in_stream;
  43.      char *buffer;
  44.      int  buffer_length;
  45. {
  46.   int x, y, text_x, text_y, dots_on=1, continued=0;
  47.   int byte_read;
  48.  
  49.   byte_read = read_byte (in_stream);
  50.   while (!feof (in_stream))
  51.     {
  52.       if (   ((byte_read>>5) == 1)
  53.       || ((byte_read>>5) == 2)
  54.       || ((byte_read>>5) == 3))
  55.     {
  56.       if ((byte_read>>5) == 1) /* high 5 bits of y */
  57.         {
  58.           y = (y & 0x1F) | ((byte_read & 0x1F)<<5);
  59.           byte_read = read_byte (in_stream);
  60.         }
  61.       if ((byte_read>>5) == 3) /* low 5 bits of y */
  62.         {
  63.           while ((byte_read>>5) == 3)
  64.         {
  65.           y = (y & (0x1F<<5)) | (byte_read & 0x1F);
  66.           byte_read = read_byte (in_stream);
  67.         }
  68.           /* only read high part of x if there was a low part of y */
  69.           if ((byte_read>>5) == 1) /* high 5 bits of x */
  70.         {
  71.           x = (x & 0x1F) | ((byte_read & 0x1F)<<5);
  72.           byte_read = read_byte (in_stream);
  73.         }
  74.           /* low 5 bits of y */
  75.           x = (x & (0x1F<<5)) | (byte_read & 0x1F);
  76.         }
  77.       text_x = x;
  78.       text_y = y;
  79.       if (continued)
  80.         {
  81.           cont (x, y);
  82.         }
  83.       else
  84.         {
  85.           move (x, y);
  86.           continued = 1;
  87.         }
  88.     }
  89.       else
  90.     switch (byte_read)
  91.       {
  92.       case '\000':        /* ignore nulls */
  93.         break;
  94.       case '\007':        /* ctrl G - BELL */
  95.         break;
  96.       case '\010':        /* ctrl H - backspace */
  97.         text_x -= 10;
  98.         break;
  99.       case '\011':        /* ctrl I - tab */
  100.         text_x += 10;
  101.         break;
  102.       case '\012':        /* ctrl J - linefeed */
  103.         text_y -= 17;
  104.         break;
  105.       case '\013':        /* ctrl K - vertical tab */
  106.         text_y += 17;
  107.         break;
  108.       case '\030':        /* alphanumerics mode */
  109.         break;
  110.       case '\015':        /* ctrl M - alphagraphics mode with reset */
  111.         text_y -= 10;
  112.         text_x = 0;
  113.         dots_on = 1;
  114.       case '\037':        /* alphagraphics mode */
  115.         {
  116.           char *p;
  117.           p = buffer;
  118.           *p = read_byte (in_stream);
  119.           while (*p > 31)
  120.         {
  121.           *(++p) = read_byte (in_stream);
  122.         }
  123.           ungetc (*p, in_stream);
  124.           if (p != buffer)
  125.         {
  126.           *p = 0;
  127.           move (text_x, text_y);
  128.           label (buffer);
  129.           text_x += strlen(buffer) * 12;
  130.           continued = 0;
  131.         }
  132.         }
  133.         break;
  134.       case '\033':
  135.         {
  136.           byte_read = read_byte (in_stream);
  137.           switch (byte_read)
  138.         {
  139.         case '\014':    /* ctrl l */
  140.           erase ();
  141.           fontsize (8);
  142.           linemod ("solid");
  143.           dots_on = 1;
  144.           x = 0;
  145.           y = 0;
  146.           break;
  147.         case '0':
  148.           fontsize (8);
  149.           break;
  150.         case '1':
  151.           fontsize (10);
  152.           break;
  153.         case '2':
  154.           fontsize (12);
  155.           break;
  156.         case '3':
  157.           fontsize (14);
  158.           break;
  159.         case '@':
  160.           fill (1);
  161.           break;
  162.         case 'A':
  163.         case 'B':
  164.         case 'C':
  165.         case 'D':
  166.         case 'E':
  167.         case 'F':
  168.         case 'G':
  169.           fill (0x7FFF);
  170.           break;
  171.         case '`':
  172.           linemod ("solid");
  173.           break;
  174.         case 'a':
  175.           linemod ("dotted");
  176.           break;
  177.         case 'b':
  178.           linemod ("dotdashed");
  179.           break;
  180.         case 'c':
  181.           linemod ("shortdashed");
  182.           break;
  183.         case 'd':
  184.           linemod ("longdashed\n");
  185.           break;
  186.         case 'x':
  187.           linemod ("user1");
  188.           break;
  189.         case 'y':
  190.           linemod ("user2");
  191.           break;
  192.         case 'z':
  193.           linemod ("user3");
  194.           break;
  195.         case '/':
  196.           {
  197.             int i=0, x[10];
  198.             sscanf("%d", (char*) &x[i]);
  199.             byte_read = read_byte (in_stream);
  200.             while (byte_read == ';')
  201.               {
  202.             i++;
  203.             sscanf("%d", (char*) &x[i]);
  204.             byte_read = read_byte (in_stream);
  205.               }
  206.             switch (byte_read)
  207.               {
  208.               case 'e':
  209.             x[0] = (x[0]%8)*45 - 90;
  210.             if (x[0] < 0)
  211.               x[0] += 360;
  212.             rotate (0, 0, x[0]);
  213.             break;
  214.               case 'd':
  215.             switch (x[0])
  216.               {
  217.               case 0:
  218.                 dots_on = 1;
  219.                 break;
  220.               case 1:
  221.                 dots_on = 0;
  222.                 break;
  223.               }
  224.             break;
  225.               case 'A': /* arc */
  226.             {
  227.               if (i < 3)
  228.                 x[3] = 0;
  229.               if (i < 4)
  230.                 x[4] = 360;
  231.               arc (x[0], x[1],
  232.                    (int) (x[0] + x[2] * cos (x[3]*M_PI/180.)),
  233.                    (int) (x[1] + x[2] * sin (x[3]*M_PI/180.)),
  234.                    (int) (x[0] + x[2] * cos ((x[3]+x[4])*M_PI/180.)),
  235.                    (int) (x[1] + x[2] * sin ((x[3]+x[4])*M_PI/180.)));
  236.             }
  237.             break;
  238.               }
  239.           }
  240.           break;
  241.         }
  242.         }
  243.         break;
  244.       case '\034':        /* point plot mode */
  245.         /* printf("point plot mode.\n");*/
  246.         break;
  247.       case '\035':        /* vector mode */
  248.         /* printf("vector mode.\n");*/
  249.         continued = 0;
  250.         break;
  251.       case '\036':        /* incremental point plot mode */
  252.         {
  253.           int x_incr = 0, y_incr = 0;
  254.           /* printf("incremental point plot: <");*/
  255.           while(!feof (in_stream) && (byte_read != '\037'))
  256.         {
  257.           byte_read = read_byte (in_stream);
  258.           switch (byte_read)
  259.             {
  260.             case ' ':    /* pen up */
  261.               dots_on = 0;
  262.               break;
  263.             case 'A':
  264.               x_incr++;
  265.               break;
  266.             case 'E':
  267.               x_incr++;
  268.               y_incr++;
  269.               break;
  270.             case 'D':
  271.               y_incr++;
  272.               break;
  273.             case 'F':
  274.               x_incr--;
  275.               y_incr++;
  276.               break;
  277.             case 'B':
  278.               x_incr--;
  279.               break;
  280.             case 'J':
  281.               x_incr--;
  282.               y_incr--;
  283.               break;
  284.             case 'H':
  285.               y_incr--;
  286.               break;
  287.             case 'I':
  288.               x_incr++;
  289.               y_incr--;
  290.               break;
  291.             case 'P':    /* pen down */
  292.               dots_on = 1;
  293.               break;
  294.             }
  295.         }
  296.           ungetc (byte_read, in_stream);
  297.           /* printf(">.\n");*/
  298.           x += x_incr;
  299.           y += y_incr;
  300.           if (x < 0)
  301.         x=0;
  302.           if (y < 0)
  303.         y=0;
  304.           move (x, y);
  305.           text_x += x_incr;
  306.           text_y += y_incr;
  307.           if (text_x < 0)
  308.         text_x=0;
  309.           if (text_y < 0)
  310.         text_y=0;
  311.         }
  312.         break;
  313.       default:
  314.         fprintf (stderr, "Unrecognized `%c' <%o %o> ignored.\n",
  315.              byte_read, byte_read & 0x1f, byte_read);
  316.       }
  317.       byte_read = read_byte (in_stream);
  318.     }
  319.   return;
  320. }
  321.  
  322.  
  323. char *progname;            /* argv[0] or the name of this program */
  324.  
  325. void
  326. display_version ()
  327. {
  328.     (void) fprintf (stderr, "\
  329. %s version %s, Copyright (C) 1989 Free Software Foundation, Inc.\n\
  330. plot comes with ABSOLUTELY NO WARRANTY; type `%s +warranty'\n\
  331. for details.  This is free software, and you are welcome to redistribute\n\
  332. it; Type `plot2ps +copying' to view the copying conditions.\n",
  333.             progname, VERS, progname);
  334. }
  335.  
  336. /* Long options we recognize */
  337. #define    ARG_NONE    0
  338. #define    ARG_REQUIRED    1
  339. #define    ARG_OPTIONAL    2
  340.  
  341. struct option    long_options[] = {
  342.     { "fontsize",    ARG_REQUIRED,    NULL, 'f' },
  343.     { "fontname",    ARG_REQUIRED,    NULL, 'F' },
  344.     { "high-byte-first", ARG_NONE,    NULL, 'h' },
  345.     { "low-byte-first",    ARG_NONE,    NULL, 'l' },
  346.     { "warranty",    ARG_NONE,    NULL, 'W' },
  347.     { "copying",    ARG_NONE,    NULL, 'C' },
  348.     { "help",        ARG_NONE,    NULL, 'H' },
  349.     { "version",    ARG_NONE,    NULL, 'V' },
  350.     { NULL,        0,        NULL, 0}
  351. };
  352.  
  353. void
  354. display_help ()
  355. {
  356.   int i;
  357.   fprintf (stderr, "usage: %s", progname);
  358.   for (i=0; long_options[i].name; i++)
  359.     {
  360.       fprintf (stderr, " [+%s", long_options[i].name);
  361.       if (isprint (long_options[i].val))
  362.     fprintf (stderr, "|-%c", long_options[i].val);
  363.       if (long_options[i].has_arg == ARG_REQUIRED)
  364.     fprintf (stderr, " arg]");
  365.       else
  366.     fprintf (stderr, "]");
  367.     }
  368.   fprintf (stderr, "\n");
  369. }
  370.  
  371.  
  372. int
  373. main (argc, argv)
  374.      int argc;
  375.      char *argv[];
  376. {
  377.   int option;
  378.   int opt_index;
  379.   int errcnt = 0;        /* errors encountered */
  380.   int show_version = 0;        /* remember to show version message */
  381.   int show_usage = 0;        /* remember whether to output usage message. */
  382.   int show_copying = 0;        /* remember to show copying conditions */
  383.   int named_input = 0;        /* count named plot files on command line. */
  384.   char *buffer;
  385.   int  buffer_length;
  386.  
  387.   progname = argv[0];
  388.   buffer_length = 1024;
  389.   buffer = (char *) malloc (buffer_length);
  390.   if (buffer <= (char *) 0)
  391.     {
  392.       perror ("malloc failed:");
  393.       exit (-1);
  394.     }
  395.  
  396.   openpl ();
  397.   space (0, 0, 767, 767);
  398.  
  399.   while ((option = getopt_long(argc, argv, "-CF:HVWf:hl", long_options, &opt_index)) != EOF) {
  400.       if (option == 0)
  401.     option = long_options[opt_index].val;
  402.  
  403.       switch (option)
  404.     {
  405.     case 'f':
  406.       /* Sizes supported by X: 8, 10, 12, 14, 18, and 24. */
  407.       (void) fontsize (atoi (optarg));
  408.     case 'F':
  409.       (void) fontname (optarg);
  410.       break;
  411.     case 'h':        /* read high byte first */
  412.       output_high_byte_first = 1;
  413.       break;
  414.     case 'l':        /* read low byte first */
  415.       output_high_byte_first = -1;
  416.       break;
  417.     case 'H':        /* Help */
  418.       show_usage = 1;
  419.       break;
  420.     case 'v':
  421.     case 'V':        /* Version */
  422.       show_version = 1;
  423.       break;
  424.     case 'W':        /* Warranty */
  425.     case 'C':        /* Copying */
  426.       show_copying = 1;
  427.       break;
  428.     case 1:
  429.       {
  430.         FILE *data_file;
  431.  
  432.         if (strcmp (optarg, "-") == 0)
  433.           data_file = stdin;
  434.         else
  435.           {
  436.         data_file = fopen (optarg, "r");
  437.         if (data_file == NULL)
  438.           {
  439.             (void) fprintf (stderr, "%s:  ignoring nonexistent or inaccessible file `%s'\n",
  440.                     argv[0], optarg);
  441.             continue;
  442.           }
  443.           }
  444.         read_plot (data_file, buffer, buffer_length);
  445.         named_input++;
  446.  
  447.         if (data_file != stdin) /* Don't close stdin */
  448.           fclose (data_file);
  449.       }
  450.       break;
  451.     default:
  452.       errcnt++;
  453.       break;
  454.     }
  455.     }
  456.  
  457.   if (show_version)
  458.     display_version ();
  459.  
  460.   if (errcnt > 0 || show_usage)
  461.     display_help ();
  462.  
  463.   if (show_copying)
  464.     {
  465.       int k;
  466.       if (!show_version)
  467.     display_version ();
  468.       for (k = 0; copy_notice[k] != '\0'; k++) {
  469.     (void) fputs (copy_notice[k], stderr);
  470.       }
  471.       closepl ();
  472.       exit (0);
  473.     }
  474.  
  475.   if (errcnt > 0 || show_usage)
  476.     {
  477.       closepl ();
  478.       exit (errcnt > 0 ? 1 : 0);
  479.     }
  480.  
  481.  
  482.   if (optind < argc)
  483.     {
  484.       for (; optind < argc; optind++)
  485.     {
  486.       FILE *data_file;
  487.  
  488.       if (strcmp (argv[optind], "-") == 0)
  489.         data_file = stdin;
  490.       else
  491.         {
  492.           data_file = fopen (argv[optind], "r");
  493.           if (data_file == NULL)
  494.         {
  495.           (void) fprintf (stderr, "%s:  ignoring nonexistent or inaccessible file `%s'\n",
  496.                   argv[0], argv[optind]);
  497.           continue;
  498.         }
  499.         }
  500.       named_input++;
  501.       read_plot (stdin, buffer, buffer_length);
  502.  
  503.       if (data_file != stdin) /* Don't close stdin */
  504.         fclose (data_file);
  505.     }
  506.     } /* endfor */
  507.  
  508.   if (!named_input)
  509.     /* Read stdin if no files were named on the command line. */
  510.     read_plot (stdin, buffer, buffer_length);
  511.  
  512.   closepl();
  513.   return 0;
  514. }
  515.